﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebUI.BL.PostCode;
using WebUI.DAL;
using System.Data;
using WebUI.BL.Member;

namespace WebUI.UserControls
{
    public partial class UCRegister : System.Web.UI.UserControl
    {
        WebFunction func = new WebFunction();
        PostCodes postcode = new PostCodes();
        DataTable dtCity;
        DataTable dtDistrict;
        DataTable dtWard;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            string day = "";
            string month = "";
            for (int d = 1; d <= 31; d++)
            {
                if (d < 10)
                    day = "0" + d;
                else
                    day = d.ToString();
                ddlday.Items.Add(day);
            }
            for (int m = 1; m <= 12; m++)
            {
                if (m < 10)
                    month = "0" + m;
                else
                    month = m.ToString();
                ddlmonth.Items.Add(month);
            }
            for (int n = DateTime.Now.Year; n >= DateTime.Now.Year - 60; n--)
                ddlyear.Items.Add(n.ToString());

            dtCity = postcode.Get_City();
            func.LoadDataToDropDownList(ddlCity, dtCity, "City", "City", false);

            dtDistrict = postcode.Get_District(ddlCity.SelectedValue);
            func.LoadDataToDropDownList(ddlDistrict, dtDistrict, "District", "District", false);

            dtWard = postcode.Get_Ward(ddlDistrict.SelectedValue);
            func.LoadDataToDropDownList(ddlWard, dtWard, "Ward", "Ward", false);
        }

        protected void ddlcity_SelectedIndexChanged(object sender, EventArgs e)
        {
            dtDistrict = postcode.Get_District(ddlCity.SelectedValue);
            func.LoadDataToDropDownList(ddlDistrict, dtDistrict, "District", "District", false);
        }

        protected void ddldistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            dtWard = postcode.Get_Ward(ddlDistrict.SelectedValue);
            func.LoadDataToDropDownList(ddlWard, dtWard, "Ward", "Ward", false);
        }

        protected void btnRegister_Click(object sender, EventArgs e)
        {
            MemberBL members = new MemberBL();
            tbl_Member member = new tbl_Member();

            string email = txtEmail.Text.Trim();
            string phone = txtPhone.Text.Trim();

            bool checkEmail = false;
            bool checkPhone = false;
            checkEmail = members.checkExistsEmail(email);
            checkPhone = members.checkExistsPhone(phone);

            if (!checkEmail)
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "ShowValidation", "javascript:alert('Email Exists.');", true);
                return;
            }

            if (!checkPhone)
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "ShowValidation", "javascript:alert('Phone Exists.');", true);
                return;
            }

            try
            {
                member.Email = email;
                member.Password = txtPassword.Text.Trim();
                string memberguid = func.HashMD5(email);
                member.MemberGuid = memberguid;
                member.MemberName = txtName.Text.Trim();
                member.Gender = Convert.ToInt32(rdGender.SelectedValue);
                member.DateBone = ddlday.SelectedValue + ddlmonth.SelectedValue + ddlyear.SelectedValue;
                member.Address = txtAddress.Text.Trim();
                string postcodename = ddlWard.SelectedValue + " " + ddlDistrict.SelectedValue + " " + ddlCity.SelectedValue;
                member.PostCodeName = postcodename;
                member.Postcode = postcode.getpostcodeid(ddlCity.SelectedValue, ddlDistrict.SelectedValue, ddlWard.SelectedValue);
                member.Phone = phone;
                member.CouponCount = 0;
                member.VisitCount = 1;
                member.OrderTotal = 0;
                member.MemberType = "NORMAL";
                member.RegisterDate = DateTime.Now;
                member.IsVerified = false;
                member.VerifyCode = func.FormatLength(memberguid, 5).Replace("...", "");
                member.Status = "NORMAL";

                if (chkCheck1.Checked)
                    member.IsMarketEmail = "1";
                else
                    member.IsMarketEmail = "0";

                members.clsMemberInsert(member);
                Response.Redirect("/Default.aspx");
            }
            catch (Exception)
            {
                ScriptManager.RegisterStartupScript(Page, typeof(Page), "ShowValidation", "javascript:alert('Error.');", true);
                return;
            }
        }
    }
}